<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 定义一个函数，输入一个链表的头节点，反转该链表并输出反转后链表的头节点。 
        // 输入: 1 -> 2 -> 3 -> 4 -> 5 -> NULL
        // 输出: 5 -> 4 -> 3 -> 2 -> 1 -> NULL

        // 迭代
        function reverseList(head) {
            var prev = null;
            var cur = head;
            while (cur) {
                var next = cur.next;
                cur.next = prev;
                prev = cur;
                cur = next;
            }
            return prev;
        }

        // 第二遍
        // abc 
        // function reverseList(head) {
        //     var prev = null;
        //     var cur = head;
        //     while (cur) {
        //         var next = cur.next;
        //         cur.next = prev;
        //         prev = cur;
        //         cur = next;
        //     }
        //     return prev;
        // }

        // 递归实现
        function reverseList(head) {
            if (!head || !head.next) {
                return head;
            }
            var next = head.next;
            var p = reverseList(next);
            next.next = head;
            head.next = null;
            return p
        }

        // 第二遍 递归 
        // 中序遍历思想
        function reverseList(head){
            if(!head || !head.next){
                return head
            }
            var next = head.next;
            var p = reverseList(next);
            next.next = head;
            head.next = null;
        }
    </script>
</body>

</html>